package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDealerInfoMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBusinessInfoMapper;
import com.ruoyi.system.domain.SysBusinessInfo;
import com.ruoyi.system.service.ISysBusinessInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.StringUtils;

/**
 * 业务信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-10
 */
@Service
public class SysBusinessInfoServiceImpl implements ISysBusinessInfoService 
{
    @Autowired
    private SysBusinessInfoMapper sysBusinessInfoMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDealerInfoMapper sysDealerInfoMapper;

    /**
     * 查询业务信息
     * 
     * @param id 业务信息ID
     * @return 业务信息
     */
    @Override
    public SysBusinessInfo selectSysBusinessInfoById(Long id)
    {
        return sysBusinessInfoMapper.selectSysBusinessInfoById(id);
    }

    /**
     * 查询业务信息列表
     * 
     * @param sysBusinessInfo 业务信息
     * @return 业务信息
     */
    @Override
    public List<SysBusinessInfo> selectSysBusinessInfoList(SysBusinessInfo sysBusinessInfo)
    {
        return sysBusinessInfoMapper.selectSysBusinessInfoList(sysBusinessInfo);
    }

    @Override
    public List<SysBusinessInfo> selectUnList(String createBy) {
        return sysBusinessInfoMapper.selectUnList(createBy);
    }

    /**
     * 新增业务信息
     * 
     * @param sysBusinessInfo 业务信息
     * @return 结果
     */
    @Override
    public int insertSysBusinessInfo(SysBusinessInfo sysBusinessInfo)
    {
        sysBusinessInfo.setStatus("0");
        return sysBusinessInfoMapper.insertSysBusinessInfo(sysBusinessInfo);
    }

    /**
     * 修改业务信息
     * 
     * @param sysBusinessInfo 业务信息
     * @return 结果
     */
    @Override
    public int updateSysBusinessInfo(SysBusinessInfo sysBusinessInfo)
    {
        sysBusinessInfo.setUpdateTime(DateUtils.getNowDate());
        return sysBusinessInfoMapper.updateSysBusinessInfo(sysBusinessInfo);
    }

    @Override
    public int updateAgreement(Long id) {
        return sysBusinessInfoMapper.updateAgreement(id);
    }

//    @Override
//    public int updateServiceContractNo(Long id)
//    {
//        SysBusinessInfo sysBusinessInfo = sysBusinessInfoMapper.selectSysBusinessInfoById(id);
//        // 机构编码+mm/dd+0001
//        SimpleDateFormat df=new SimpleDateFormat("MMdd");
//        String dealerName = sysDeptMapper.selectDeptById(sysUserMapper.selectUserById(Long.valueOf(sysBusinessInfo.getCreateBy())).getDeptId()).getDeptName();
//        String organCode = sysDealerInfoMapper.selectOrganCodeByDealerName(dealerName);
//
//        String newestServiceContractNo = sysBusinessInfoMapper.selectNewestServiceContractNo();
//        if(StringUtils.isEmpty(newestServiceContractNo)){
//            sysBusinessInfo.setServiceContractNo(organCode + df.format(new Date()) + String.format("%04d",1));
//        }else {
//            if(df.format(new Date()).equals(newestServiceContractNo.substring(5,9))){
//                sysBusinessInfo.setServiceContractNo(organCode + df.format(new Date()) + String.format("%04d", Integer.valueOf(newestServiceContractNo.substring(9,13)) + 1));
//            }else {
//                sysBusinessInfo.setServiceContractNo(organCode + df.format(new Date()) + String.format("%04d",1));
//            }
//        }
//
//        return sysBusinessInfoMapper.updateSysBusinessInfo(sysBusinessInfo);
//    }

    @Override
    public int updateStatus(Long id){
        return sysBusinessInfoMapper.updateStatus(id);
    }

    /**
     * 删除业务信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysBusinessInfoByIds(String ids)
    {
        return sysBusinessInfoMapper.deleteSysBusinessInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除业务信息信息
     * 
     * @param id 业务信息ID
     * @return 结果
     */
    @Override
    public int deleteSysBusinessInfoById(Long id)
    {
        return sysBusinessInfoMapper.deleteSysBusinessInfoById(id);
    }
}
